﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Controls.DataVisualization.Charting;


namespace FRL.DebtChased.Controls
{
    public partial class ReportsAndStatsCtrl : UserControl, INotifyPropertyChanged
    {

        #region Constructor
        public ReportsAndStatsCtrl()
        {
            InitializeComponent();
            _refreshStats();
            this.LayoutRoot.DataContext = this;
            RaisePropertyChanged("RecordTabs");
        }
        #endregion

        #region Temp Tab Handling
        public IEnumerable<TabItem> RecordTabs
        {
            get
            {
                List<TabItem> tabs = new List<TabItem>();
                tabs.Add(new TabItem() { Header = "Report Management", Tag="0" });
                tabs.Add(new TabItem() { Header = "Client Dashboard", Tag = "1" });
                return tabs;
            }
        }
        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabControl ctrl = (TabControl)sender;
            if (ctrl.SelectedIndex >= 0)
            {
                TabItem tab = (TabItem)ctrl.Items[ctrl.SelectedIndex];
                ManagementTab.Visibility = (tab.Tag == "0") ? Visibility.Visible : Visibility.Collapsed;
                DashboardTab.Visibility = (tab.Tag == "1") ? Visibility.Visible : Visibility.Collapsed;
            }
        }
        #endregion

        #region INotifyPropertyChanged handlers
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        #region Record Control
        private bool _isRecordEditable;
        public bool IsRecordEditable
        {
            get { return _isRecordEditable; }
            set
            {
                if (value != _isRecordEditable)
                {
                    _isRecordEditable = value;
                    RaisePropertyChanged("IsRecordEditable");
                    RaisePropertyChanged("EditRecordVisibility");
                    RaisePropertyChanged("ViewRecordVisibility");
                }
            }
        }
        public Visibility EditRecordVisibility
        {
            get { return _isRecordEditable ? Visibility.Visible : Visibility.Collapsed; }
        }
        public Visibility ViewRecordVisibility
        {
            get { return _isRecordEditable ? Visibility.Collapsed : Visibility.Visible; }
        }

        #endregion

        #region Form Event Handlers
        private GridLength _gridLengthZero = new GridLength(0);
        private GridLength _gridLengthAuto = new GridLength(1, GridUnitType.Auto);

        private void HeaderBar_ExpandChange(object sender, EventArgs e)
        {
            HeaderBar.IsArrowUp = !HeaderBar.IsArrowUp;
            Body.Visibility = HeaderBar.IsArrowUp ? Visibility.Visible : Visibility.Collapsed;
        }
        private void ToggleEditView_Click(object sender, EventArgs e)
        {
            IsRecordEditable = !IsRecordEditable;
        }

        private void butRequestModification_Click(object sender, EventArgs e)
        {

        }

        private void cboContact_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        #endregion

        #region Old Stats Bit
        		public class StatRecord
		{

			/////////////////////////////////////////////////////////
			// Attributes

			private string m_strSection;
			public string Section
			{
				get { return m_strSection; }
			}

			private double m_dblTotal;
			public double Total
			{
				get { return m_dblTotal; }
			}

			/////////////////////////////////////////////////////////
			// Construction

			public StatRecord(string strSection, double dblTotal)
			{

				// Initialize member variables
				m_strSection = strSection;
				m_dblTotal = dblTotal;

			}

		}

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<StatRecord> m_stats = new List<StatRecord>();
        private List<StatRecord> _stats2 = new List<StatRecord>();

		/////////////////////////////////////////////////////////////
		// Construction

        /*
		public ClientStatsCtrl()
		{

			// Load xaml
			InitializeComponent();

			// Set controls - Stats Type
			ComboBoxObject.LoadFromEnumeration(cboStatsType, typeof(Definitions.eClientStatsType));
			cboStatsType.SelectedIndex = 0;

			// Refresh the data
			_refreshStats();

		}
        /**/

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void butEmailFrequency_Click(object sender, RoutedEventArgs e)
		{

			// TODO
			// Insert frequency editing control developed in ChaseWorkspace plugin.

		}

		private void butRefresh_Click(object sender, RoutedEventArgs e)
		{

			// Refresh the data
			_refreshStats();

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private void _refreshStats()
		{

			// Generate random data
			Random rnd = new Random();

			// Load the stats (use fake data for display)
			m_stats = new List<StatRecord>();
			m_stats.Add(new StatRecord("Chase", rnd.Next(2,30)));
			m_stats.Add(new StatRecord("Query", rnd.Next(2, 30)));
			m_stats.Add(new StatRecord("Copy Invoice", rnd.Next(2, 30)));
			m_stats.Add(new StatRecord("Expected", rnd.Next(2, 30)));
			m_stats.Add(new StatRecord("LBA", rnd.Next(2, 30)));
			m_stats.Add(new StatRecord("Legal", rnd.Next(2, 30)));

			// Bind to series
			PieSeries pieSeries = chart.Series[0] as PieSeries;
			pieSeries.ItemsSource = m_stats;

            _stats2 = new List<StatRecord>();
            _stats2.Add(new StatRecord("Paid Ex Interest", rnd.Next(2, 30)));
            _stats2.Add(new StatRecord("Paid in Full Inc Interest", rnd.Next(2, 30)));
            _stats2.Add(new StatRecord("Uneconomic To Collect", rnd.Next(2, 30)));

            // Bind to series
            PieSeries pieSeries2 = chart2.Series[0] as PieSeries;
            pieSeries2.ItemsSource = _stats2;



		}

        #endregion

    }
}
